/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    AndroidSDK
 *
 *    CoverFlowTest
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-12-29
 *    @version: 1.0
 *
 ******************************************************************************/

package com.android.sdk;

import com.sdk.util.Log;
import com.sdk.widget.CoverFlow;
import com.sdk.widget.MyImgView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class CoverFlowTest extends Activity {
    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLauncher();
        final CoverFlow cf = new CoverFlow(this);
        cf.setOnItemClickListener(mItemClickListener);
        cf.setOnItemSelectedListener(mOnItemSelectedListener);
        cf.setAdapter(new ImageAdapter(this));
        ImageAdapter imageAdapter = new ImageAdapter(this);
        cf.setAdapter(imageAdapter);// �Զ���ͼƬ����䷽ʽ
        cf.setAnimationDuration(1500);
        setContentView(cf);
    };

    List<Drawable> lvalue;

    public void getLauncher() {
        lvalue = new ArrayList<Drawable>();

        PackageManager pkgMgt = this.getPackageManager();
        Intent it = new Intent(Intent.ACTION_MAIN);
        it.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);// ��ѯ
        for (int i = 0; i < ra.size(); i++) {
            ActivityInfo ai = ra.get(i).activityInfo;
            Drawable icon = ai.loadIcon(pkgMgt);
            lvalue.add(icon);
        }

    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;

        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return lvalue.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return composeItem(position);
        }

        public View composeItem(int position) {

            ImageView iv = new ImageView(mContext);
            iv.setImageDrawable(lvalue.get(position));

            iv.setImageBitmap(MyImgView.createReflectedImage(MyImgView.drawableToBitmap(lvalue
                    .get(position))));// ���봦����ͼƬ
            iv.setLayoutParams(new Gallery.LayoutParams(80, 60));
            return iv;
        }
    }

    private OnItemClickListener mItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i(TAG, " [onItemClick] position = " + position);
        }
    };

    private OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.i(TAG, " [onItemSelected] position = " + position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Log.i(TAG, " [onNothingSelected] ");
        }
    };

    private static final String TAG = CoverFlowTest.class.getSimpleName();
}
